package main;

import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import jade.core.Agent;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREInitiator;
import jade.proto.AchieveREResponder;

public class MapAgent extends Agent {
	

	
	@Override
	protected void setup() {
		super.setup();
		addBehaviour(new LocationInfo(this));
	}
	
	static class LocationInfo extends AchieveREResponder
	{
		
		/**
		 * 
		 */
		//Agent only respond for a certain requests
		private static final MessageTemplate mt = MessageTemplate.and(
					MessageTemplate.MatchProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST), 
					MessageTemplate.MatchPerformative(ACLMessage.REQUEST));;
	
		private final long serialVersionUID = 740028759853592117L;
					
		public LocationInfo(Agent a) {
			super(a, mt);	
		}
		
		
		@Override
		protected ACLMessage handleRequest(ACLMessage request)
				throws NotUnderstoodException, RefuseException {
			ACLMessage msg = request.createReply();
			msg.setPerformative(ACLMessage.AGREE);
			return msg;
			
		}
		
		@Override
		protected ACLMessage prepareResultNotification(ACLMessage request,
				ACLMessage response) throws FailureException {
			
			ACLMessage inform = request.createReply();
			inform.setPerformative(ACLMessage.INFORM);
			try {
				inform.setContentObject(System.currentTimeMillis());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return inform;
		}
		
	}
}
